﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Drawing;

using Elegant.Ui;

namespace iPdf
{
    /// <summary>
    /// The DropDown menu control with customized items to fit our need
    /// </summary>
    partial class TataDropDown: Elegant.Ui.SplitButton
    {
        #region Fields
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        protected Collection<TataDropDownItem> attachItems;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        protected string selectedItem = string.Empty;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        protected Button defaultMenuItem ;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        protected int maxItemsCount = 10;
        #endregion

        #region Properties
        public string SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
                RefreshMenuItems();
            }
        }

        public Elegant.Ui.ListWithEvents<System.Windows.Forms.Control> DropDownMenuItems
        {
            get
            {
                PopupMenu popupMenu =  (PopupMenu)this.Popup;
                return popupMenu.Items;
            }
        }

        public int MaxItemsCount
        {
            get
            {
                return maxItemsCount;
            }
            set
            {
                maxItemsCount = value;
            }
        }

        public Button DefaultMenuItem
        {
            get
            {
                return defaultMenuItem;
            }
            set
            {
                defaultMenuItem = value;
            }
        }
        #endregion 

        #region Methods
        protected void LoadDefaultMenuItem()
        {
            if (defaultMenuItem == null)
            {
                defaultMenuItem = new Button();
                defaultMenuItem.Text = this.Text;
                defaultMenuItem.CommandName = this.CommandName;
                defaultMenuItem.SmallImages.Images.AddRange(this.SmallImages.Images.ToArray());
            }
            DropDownMenuItems.Add(defaultMenuItem);

            selectedItem = string.Empty;
        }

        public virtual void RefreshMenuItems()
        {
            DropDownMenuItems.Clear();
            List<TataDropDownItem> invalidItems = new List<TataDropDownItem>();
            foreach (TataDropDownItem item in attachItems)
            {
                if (item.IsValid)
                {
                    CreatePopupMenuItem(item);
                }
                else
                {
                    invalidItems.Add(item);
                }
            }

            foreach (TataDropDownItem invalidItem in invalidItems)
            {
                attachItems.Remove(invalidItem);
            }

            if (DropDownMenuItems.Count == 0)
            {
                LoadDefaultMenuItem();
            }
        }


        public void CreatePopupMenuItem(TataDropDownItem item)
        {
            if (Contains(item) || !item.IsValid)
            {
                return;
            }

            if( attachItems.Count == maxItemsCount)
            {
                attachItems.RemoveAt( 0 );
            }
            if (!attachItems.Contains(item))
            {
                attachItems.Add(item);
            }

            if (DropDownMenuItems.Count == maxItemsCount)
            {
                DropDownMenuItems.RemoveAt(0);
            }

            Elegant.Ui.ToggleButton menuItem = new Elegant.Ui.ToggleButton();
            menuItem.Id = Helper.GenerateGuid();
            menuItem.Name = this.Name + DropDownMenuItems.Count.ToString();
            menuItem.Pressed = item.Equals(selectedItem);
            menuItem.Text = item.Text;
            menuItem.Click += item.Click;
            menuItem.Tag = item;
            DropDownMenuItems.Add(menuItem);
        }

        public void AddItem(TataDropDownItem item)
        {
            CreatePopupMenuItem(item);
        }

        public void AddItem(string item, EventHandler clickHandler)
        {
            TataDropDownItem tataDropDownItem = new TataDropDownItem(this, item, clickHandler);
            AddItem(tataDropDownItem);
        }

        public void AddItems(TataDropDownItem[] items)
        {
            foreach (TataDropDownItem item in items)
            {
                CreatePopupMenuItem(item);
            }
        }

        public bool Contains(TataDropDownItem item)
        {
            return Contains(item.Text);
        }

        public bool Contains(string item)
        {
            if (DropDownMenuItems.Count == 0)
            {
                return false;
            }

            foreach (Elegant.Ui.Control element in DropDownMenuItems)
            {
                if (element.Text == item)
                {
                    return true;
                }
            }

            return false;
        }

        public void AddStringArray(string[] items, EventHandler clickHandler)
        {
            if(items != null)
            {
                foreach (string item in items)
                {
                    TataDropDownItem tataDropDownItem = new TataDropDownItem(this, item, clickHandler);
                    CreatePopupMenuItem(tataDropDownItem);
                }
            }
        }

        /// <summary>
        /// Convert the attach items to string array
        /// </summary>
        /// <returns></returns>
        public virtual string[] ToStringArray()
        {
            List<string> strList = new List<string>();

            foreach (TataDropDownItem item in attachItems)
            {
                if (item.IsValid)
                {
                    strList.Add(item.Text);
                }
            }

            return strList.ToArray();
        }
        #endregion
    }
}
